package cn.kt.controller.UserTeacher;

import cn.kt.common.vo.ktResult;
import cn.kt.pojo.TeacherPojo;
import cn.kt.service.UserTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserUpdateController {

    @Autowired
    UserTeacherService userTeacherService;

    //展示修改页面
    @GetMapping("/teacher-modify/id/{id}")
    public String userTeacherModify(Model model, TeacherPojo tpValue){
        TeacherPojo teacherPojo = new TeacherPojo();

        TeacherPojo tp = userTeacherService.findUser(tpValue);
        model.addAttribute("tp",tp);
        return "admin/index/teacher-modify";
    }

    //更新数据
    @PutMapping("/userTeacherUpdate")
    @ResponseBody
    public ktResult userTeacherUpdate(TeacherPojo tp){

        return userTeacherService.updateUser(tp);
    }

    //校验用户名，如果传入的用户名与id查询出来的不一致，就检验是否被占用
    @GetMapping("/teacher-upDateCheckUser")
    @ResponseBody
    public ktResult updateCheckAccount(TeacherPojo tp){
        TeacherPojo tpObject = userTeacherService.findUser(tp);
        if (tpObject == null){
            return userTeacherService.checkUser(tp.getAccount());
        }
        return ktResult.success();
    }

    //校验手机号
    @GetMapping("/teacher-updateCheckPhone")
    @ResponseBody
    public ktResult updateCheckPhone(TeacherPojo tp){
        TeacherPojo tpObject = userTeacherService.findUser(tp);
        if (tpObject == null){
            return userTeacherService.checkPhone(tp.getPhone());
        }
        return ktResult.success();
    }

}
